package com.ayh.campusfun.controller.api.v1;

import com.ayh.campusfun.common.CommonResult;
import com.ayh.campusfun.common.PageResult;
import com.ayh.campusfun.common.core.jwt.JWTUtils;
import com.ayh.campusfun.entity.Message;
import com.ayh.campusfun.service.MessageService;
import com.ayh.campusfun.vo.MessageVo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息表(Message)表控制层
 *
 * @author ayh
 * @since 2021-03-20 20:42:39
 */
@RestController
@RequestMapping("message")
public class MessageController {
    /**
     * 服务对象
     */
    @Autowired
    private MessageService messageService;

    @Resource
    private JWTUtils jwtUtils;

    @PostMapping("/getUnreadMessage")
    public CommonResult getUnreadMessage(HttpServletRequest request){
        String uid = jwtUtils.getUidFromRequest(request);
        List<MessageVo> messageVoList = messageService.findUnreadMessageFriendId(uid);
        Map<String, Object> param = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        messageVoList.forEach(item -> {
            param.clear();
            param.put("toFriendId",item.getToFriendId());
            param.put("uid", item.getUid());
            param.put("messageStatus",2);
            param.put("order","send_time asc");
            List<Message> listByParam = messageService.findListByParam(param);
            item.setMessageList(mapper.convertValue(listByParam, new TypeReference<List<Message>>() {
            }));
        });
        return CommonResult.success(messageVoList);
    }

    @PostMapping("/getMessageByFriendUid")
    public CommonResult getMessageByFriend(HttpServletRequest request, String friendUid,
                                           @RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "30") Integer limit){
        String uid = jwtUtils.getUidFromRequest(request);
        Map<String, Object> map = new HashMap<>(6);
        map.put("page",page);
        map.put("limit",limit);
        map.put("friendUid",friendUid);
        map.put("uid",uid);
        map.put("order","send_time desc");
        PageResult<Message> messageList =  messageService.findMessageByFriendUid(map);
        Collections.reverse(messageList.getData());
        return CommonResult.success(messageList);
    }
    
    @PostMapping("/readMessage")
    public CommonResult readMessage(HttpServletRequest request, String friendUid){
        String uid = jwtUtils.getUidFromRequest(request);
        Long update = messageService.updateMessageStatus(uid, friendUid);
        return CommonResult.success(update);
    }

}
